package com.kungfuhacking.wristbandpro.location.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.kungfuhacking.wristbandpro.R;
import com.kungfuhacking.wristbandpro.base.view.BaseActivity;
import com.kungfuhacking.wristbandpro.custom.TitleBarView;
import com.kungfuhacking.wristbandpro.location.bean.AreaListEntity;
import com.kungfuhacking.wristbandpro.location.presenter.ISafeAreaPresenter;
import com.kungfuhacking.wristbandpro.location.presenter.SafeAreaAdapter;
import com.kungfuhacking.wristbandpro.location.view.ISafeAreaListView;
import com.kungfuhacking.wristbandpro.utils.LogUtil;
import com.kungfuhacking.wristbandpro.widget.swipe.DividerItemDecoration;
import com.kungfuhacking.wristbandpro.widget.swipe.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

import static com.kungfuhacking.wristbandpro.R.id.tv_add;

/**
 * 安全区域列表页面
 * Created by Aaron on 17/5/5.
 */

public class SafeAreaListActivity extends BaseActivity implements ISafeAreaListView, View.OnClickListener, SwipeRefreshLayout.OnRefreshListener {

    private com.kungfuhacking.wristbandpro.custom.TitleBarView tbv;
    private android.support.v7.widget.RecyclerView recycleView;
    private android.widget.ImageView ivadd;
    private android.widget.TextView tvadd;
    private ISafeAreaPresenter iSafeAreaPresenter = new ISafeAreaPresenter(this);
    private List<AreaListEntity> areaLists = new ArrayList<>();
    private SafeAreaAdapter safeAreaAdapter;
    private String TAG = getClass().getSimpleName();
    private SwipeRefreshLayout swipeRefreshLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_white_tel);
        initWidget();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //获取安全围栏列表
        iSafeAreaPresenter.getSafeArea();
    }

    private void initWidget() {

        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
        //为SwipeRefreshLayout设置监听事件
        swipeRefreshLayout.setOnRefreshListener(this);
//        //为SwipeRefreshLayout设置刷新时的颜色变化，最多可以设置4种
        swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);

        this.tvadd = (TextView) findViewById(tv_add);
        this.ivadd = (ImageView) findViewById(R.id.iv_add);
        this.recycleView = (RecyclerView) findViewById(R.id.rv_list);
        this.tbv = (TitleBarView) findViewById(R.id.tbv);
        tbv.setTitle("安全区域");
        tbv.setOnLeftClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tvadd.setText("添加安全围栏");
        initRecycleView();
        ivadd.setOnClickListener(this);
    }

    private void initRecycleView() {

        recycleView.setLayoutManager(new LinearLayoutManager(this));
        recycleView.setItemAnimator(new DefaultItemAnimator());
        safeAreaAdapter = new SafeAreaAdapter(this, areaLists);
        recycleView.setAdapter(safeAreaAdapter);
        recycleView.addItemDecoration(new DividerItemDecoration(this,
                DividerItemDecoration.VERTICAL_LIST,
                getResources().getDrawable(R.color.bg_gray)));

        recycleView.addOnItemTouchListener(new OnItemClickListener(recycleView) {
            @Override
            public void onItemClick(RecyclerView.ViewHolder holder, int position) {
                //进入安全区域页面
                Bundle bundle = new Bundle();
                bundle.putParcelable("areaDetail", areaLists.get(position));
                nextActivityWithBundle(SafeDetailActivity.class, bundle);
            }

            @Override
            public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

            }
        });
        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new ItemTouchHelper.Callback() {
            @Override
            public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
//dragFlags的时候，就会在长按item的时候进入拖动模式，然后就会一直回调onMove函数。我们可以在onMove中进行数据集的更新
                final int dragFlags;//用于拖拽的标志 如果是网格布局，拖动方向为上下左右，否则只有上下方向
                final int swipeFlags;//滑动标记
                if (recyclerView.getLayoutManager() instanceof GridLayoutManager) {
                    dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
                    swipeFlags = 0;
                } else {
                    dragFlags = 0;
                    swipeFlags = ItemTouchHelper.START;
                }
                return makeMovementFlags(dragFlags, swipeFlags);
            }

            @Override
            public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
                return false;
            }

            @Override
            public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
                int position = viewHolder.getAdapterPosition();
                iSafeAreaPresenter.deleteSafeArea(areaLists.get(position).getId());
            }

        });
        itemTouchHelper.attachToRecyclerView(recycleView);
    }

    @Override
    public void setAreaData(List<AreaListEntity> areaLists) {
        LogUtil.d(TAG, " 安全区域 : " + areaLists.size());
        this.areaLists = areaLists;
        safeAreaAdapter.setAreaListEntitys(areaLists);
        safeAreaAdapter.notifyDataSetChanged();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        iSafeAreaPresenter.clearRequest();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.iv_add://添加安全围栏
                nextActivity(SafeAreaAddActivity.class);
                break;

        }
    }

    @Override
    public void onRefresh() {

        areaLists.clear();
        safeAreaAdapter.setAreaListEntitys(areaLists);
        safeAreaAdapter.notifyDataSetChanged();
        iSafeAreaPresenter.getSafeArea();
        //结束后停止刷新
        swipeRefreshLayout.setRefreshing(false);

    }
}
